package cc.comac.mom.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cc.comac.mom.api.EquipDevTypeService;
import cc.comac.mom.bean.EquipDevType;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.entity.EquipDevTypeEntity;
import cc.comac.mom.util.BusinessKey;
import cc.comac.mom.util.ExcelUploadUtil;
import cc.comac.mom.util.ResultBean;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/EquipDevType")
public class EquipDevTypeController {

	@Autowired
	private EquipDevTypeService equipDevTypeService;

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public Object insert(@RequestBody EquipDevType equipDevType) {
		ResultBean insert = equipDevTypeService.insert(equipDevType);
		return insert;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResultBean update(@RequestBody EquipDevType equipDevType) {
		ResultBean update = equipDevTypeService.update(equipDevType);
		return update;
	}

	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public ResultBean batchDelete(@RequestBody List<EquipDevType> equipDevTypeList) {
		ResultBean batchDelete = equipDevTypeService.batchDelete(equipDevTypeList);
		return batchDelete;
	}

	// 分页查询所有
	@RequestMapping(value = "/selectAllByPage", method = RequestMethod.POST)
	public ResultBean selectAllByPage(@RequestBody EquipDevType equipDevType) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev selectAllByPage = equipDevTypeService.selectAllByPage(equipDevType);
		resultBean.setObject(selectAllByPage);
		return resultBean;
	}

	// 查询按条件条件
	@RequestMapping(value = "/selectBySomeField", method = RequestMethod.POST)
	public ResultBean selectBySomeField(@RequestBody EquipDevType equipDevType) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev selectBySomeField = equipDevTypeService.selectBySomeField(equipDevType);
		resultBean.setObject(selectBySomeField);
		return resultBean;
	}

	// 查询所有已启用数据
	@RequestMapping(value = "/getAllEnable", method = RequestMethod.POST)
	public ResultBean getAllEnable() {
		ResultBean resultBean = new ResultBean(true);
		List<EquipDevTypeEntity> allEnable = equipDevTypeService.getAllEnable();
		resultBean.setObject(allEnable);
		return resultBean;
	}

	@ApiOperation(value = "设备主数据导出模板下载")
	@RequestMapping(value = "/TemplateDownload",method = RequestMethod.POST)
	public @ResponseBody void TemplateDownload(HttpServletResponse request, HttpServletResponse response)
			throws Exception {

		String fileName = "备件类型" + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
		ArrayList<String> listName = new ArrayList<String>();
		listName.add("类型编码");
		listName.add("类型名称");
		listName.add("类型备注");
		listName.add("是否启用(1:启用,0:不启用)");
		ExcelUploadUtil.TemplateDownload(request, response, fileName, listName);
	}

	@ApiOperation(value = "备件类型导入数据")
	@RequestMapping(value = "/TemplateDataUpload", method = RequestMethod.POST)
	public @ResponseBody ResultBean dataUpload(@RequestPart("file") MultipartFile file, HttpServletRequest request)
			throws IOException {
		// 文件所在的临时文件夹
		String rootPath = request.getServletContext().getRealPath("/") + "uploadExcel";
		ResultBean resultBean2 = equipDevTypeService.importEquipDevType(file, rootPath);
		return resultBean2;
	}
}
